/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backbeans;

import entity.Autor;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import provider.JPAUtils;

/**
 *
 * @author eco
 */
public class AutorFaces implements Serializable {
    
    private Autor autor;
    
    private EntityManager em;
    
    @PostConstruct
    public void init(){
    }

    public List<Autor> getAutores() {
        em = JPAUtils.getFactory().createEntityManager();
        Query query = em.createQuery("select a from Autor a");
        return query.getResultList();
    }

    public Autor getAutor() {
        return autor;
    }

    public void setAutor(Autor autor) {
        this.autor = autor;
    }   
    
    public String adicionar(){
        autor = new Autor();
        return "cadastro";
    }

    public String salva(){
        em.getTransaction().begin();
        if(autor.getId() != null){
           em.merge(autor); 
        }else{
            em.persist(autor);
        }
        em.getTransaction().commit();
        return "lista?faces-redirect=true";
    }
    
    public String edit(Autor autor){
        setAutor(autor);
        return "cadastro";
    }
}
